﻿// -----------------------------------------------------------------------
// <copyright file="NodeWorkflowRollData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db NodeWorkflowRoll
    /// </summary>
    public class NodeWorkflowRollData
    {
        /// <summary>
        /// Get NodeWorkflowRoll By Id
        /// </summary>
        /// <param name="nodeWorkflowRollId">Param nodeWorkflowRollId</param>
        /// <returns>Return NodeWorkflowRoll</returns>
        public NodeWorkflowRollEN GetNodeWorkflowRollById(int nodeWorkflowRollId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                NodeWorkflowRollEN nodeWorkflowRoll = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWorkflowRollGetById, nodeWorkflowRollId);
                while (reader.Read())
                {
                    nodeWorkflowRoll = new NodeWorkflowRollEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeWorkflowRoll.NodeWorkflowRollId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeWorkflowRoll.NodeWorkflowId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeWorkflowRoll.DepartmentId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        nodeWorkflowRoll.RoleName = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        nodeWorkflowRoll.ToCc = reader.GetBoolean(4);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return nodeWorkflowRoll;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all NodeWorkflowRoll
        /// </summary>
        /// <returns>Return list all NodeWorkflowRoll</returns>
        public IList<NodeWorkflowRollEN> GetNodeWorkflowRolls()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<NodeWorkflowRollEN> lstNodeWorkflowRoll = new List<NodeWorkflowRollEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWorkflowRollGetAll);
                while (reader.Read())
                {
                    NodeWorkflowRollEN nodeWorkflowRoll = new NodeWorkflowRollEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeWorkflowRoll.NodeWorkflowRollId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeWorkflowRoll.NodeWorkflowId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeWorkflowRoll.DepartmentId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        nodeWorkflowRoll.RoleName = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        nodeWorkflowRoll.ToCc = reader.GetBoolean(4);
                    }

                    lstNodeWorkflowRoll.Add(nodeWorkflowRoll);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstNodeWorkflowRoll;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db NodeWorkflowRoll
        /// </summary>
        /// <param name="nodeWorkflowRoll">Paarm NodeWorkflowRoll</param>
        /// <returns>Insert NodeWorkflowRoll</returns>
        public int Insert(NodeWorkflowRollEN nodeWorkflowRoll)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.NodeWorkflowId, nodeWorkflowRoll.NodeWorkflowId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.DepartmentId, nodeWorkflowRoll.DepartmentId != 0 ? nodeWorkflowRoll.DepartmentId : (object)DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.RoleName, nodeWorkflowRoll.RoleName),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.ToCc, nodeWorkflowRoll.ToCc)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWorkflowRollInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db NodeWorkflowRoll
        /// </summary>
        /// <param name="nodeWorkflowRoll">Param NodeWorkflowRoll</param>
        public void Update(NodeWorkflowRollEN nodeWorkflowRoll)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.NodeWorkflowRollId, nodeWorkflowRoll.NodeWorkflowRollId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.NodeWorkflowId, nodeWorkflowRoll.NodeWorkflowId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.DepartmentId, nodeWorkflowRoll.DepartmentId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.RoleName, nodeWorkflowRoll.RoleName),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.ToCc, nodeWorkflowRoll.ToCc)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWorkflowRollUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete NodeWorkflowRoll by nodeWorkflowRollId
        /// </summary>
        /// <param name="nodeWorkflowRollId">param nodeWorkflowRollId</param>
        public void Delete(int nodeWorkflowRollId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.NodeWorkflowRollId, nodeWorkflowRollId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWorkflowRollDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List NodeWorkflowRoll By NodeWorkflowId
        /// </summary>
        /// <param name="nodeWorkflowId">Param nodeWorkflowId</param>
        /// <returns>Return List NodeWorkflowRoll</returns>
        public IList<NodeWorkflowRollEN> GetNodeWorkflowRollByNodeWorkflowId(int nodeWorkflowId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<NodeWorkflowRollEN> lstNodeWorkflowRoll = new List<NodeWorkflowRollEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWorkflowRollGetByNodeWorkflowId, nodeWorkflowId);
                while (reader.Read())
                {
                    NodeWorkflowRollEN nodeWorkflowRoll = new NodeWorkflowRollEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeWorkflowRoll.NodeWorkflowRollId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeWorkflowRoll.NodeWorkflowId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeWorkflowRoll.DepartmentId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        nodeWorkflowRoll.RoleName = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        nodeWorkflowRoll.ToCc = reader.GetBoolean(4);
                    }

                    lstNodeWorkflowRoll.Add(nodeWorkflowRoll);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstNodeWorkflowRoll;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List NodeWorkflowRoll By DepartmentId
        /// </summary>
        /// <param name="departmentId">Param departmentId</param>
        /// <returns>Return List NodeWorkflowRoll</returns>
        public IList<NodeWorkflowRollEN> GetNodeWorkflowRollByDepartmentId(int departmentId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<NodeWorkflowRollEN> lstNodeWorkflowRoll = new List<NodeWorkflowRollEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWorkflowRollGetByDepartmentId, departmentId);
                while (reader.Read())
                {
                    NodeWorkflowRollEN nodeWorkflowRoll = new NodeWorkflowRollEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeWorkflowRoll.NodeWorkflowRollId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeWorkflowRoll.NodeWorkflowId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeWorkflowRoll.DepartmentId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        nodeWorkflowRoll.RoleName = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        nodeWorkflowRoll.ToCc = reader.GetBoolean(4);
                    }

                    lstNodeWorkflowRoll.Add(nodeWorkflowRoll);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstNodeWorkflowRoll;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Exist code
        /// </summary>
        /// <param name="roleName">Param roleName</param>
        /// <returns>Return Exist</returns>
        public bool Exist(int NodeWorkflowId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.NodeWorkflowId, NodeWorkflowId)
                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWorkflowRollExist, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Exist code
        /// </summary>
        /// <param name="roleName">Param roleName</param>
        /// <returns>Return Exist</returns>
        public bool Exist_Roll(int DepartmentId, string roleName, int NodeWorkflowId, bool toCc)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.DepartmentId, DepartmentId),
                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.RoleName, roleName),
                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.NodeWorkflowId, NodeWorkflowId),
                    new SqlParameter(Constants.SqlParamerterName.NodeWorkflowRoll.ToCc, toCc)
                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWorkflowRollExistRoll, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets the node workflow roll by node workflow_ to cc.
        /// </summary>
        /// <param name="nodeWorkflowId">The node workflow id.</param>
        /// <param name="tocc">if set to <c>true</c> [tocc].</param>
        /// <param name="profileTypeId">The profile type id.</param>
        /// <returns>
        /// Return List NodeWorkflowRoll
        /// </returns>
        public IList<NodeWorkflowRollEN> GetNodeWorkflowRollByNodeWorkflow_ToCc(int nodeWorkflowId, bool tocc, int profileTypeId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<NodeWorkflowRollEN> lstNodeWorkflowRoll = new List<NodeWorkflowRollEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWorkflowRollGetByNodeWorkflow_ToCc, nodeWorkflowId, tocc, profileTypeId);
                while (reader.Read())
                {
                    NodeWorkflowRollEN nodeWorkflowRoll = new NodeWorkflowRollEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeWorkflowRoll.NodeWorkflowRollId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeWorkflowRoll.NodeWorkflowId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeWorkflowRoll.DepartmentId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        nodeWorkflowRoll.RoleName = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        nodeWorkflowRoll.ToCc = reader.GetBoolean(4);
                    }

                    lstNodeWorkflowRoll.Add(nodeWorkflowRoll);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstNodeWorkflowRoll;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}